package com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.innodroid.expandablerecycler.ExpandableRecyclerAdapter;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.model.entity.LiYiChuKuList;
import com.xiaoxing.zhinenghuacangku_liaocheng.R;

import java.util.List;
import java.util.Map;


public class LiYiChuKuAdapter extends ExpandableRecyclerAdapter<LiYiChuKuAdapter.OrderListItem> {
    public static final int TYPE_CANG_KU_NAME = 1000; //仓库名称
    public static final int TYPE_CANG_KU_PRODUCTS = 1001; //仓库商品名称
    public static final int TYPE_FOOTER = 1002; //商家订单底部统计

    private List<LiYiChuKuList> mGroups;
    private Map<String, List<LiYiChuKuList.OutListBean>> mChildren;
    private Map<Integer, Integer> mGroupsPosition;

    private CheckInterface checkInterface;
    private ChuKuInterface mChuKuInterface;

    private Handler handler;
    private Runnable runnable;


    public LiYiChuKuAdapter(Context context, List<LiYiChuKuList> groups, Map<String, List<LiYiChuKuList.OutListBean>> children, Map<Integer, Integer> groupsPosition) {
        super(context);
        this.mGroups = groups;
        this.mChildren = children;
        this.mGroupsPosition = groupsPosition;
        handler = new Handler();
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setChuKuInterface(ChuKuInterface chuKuInterface) {
        this.mChuKuInterface = chuKuInterface;
    }

    public Object getChild(int groupPosition, int childPosition) {
        List<LiYiChuKuList.OutListBean> childs = mChildren.get(String.valueOf(groupPosition));
        return childs.get(childPosition);
    }

    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_CANG_KU_NAME:
                return new ShangJiaNameViewHolder(inflate(R.layout.zhi_neng_cang_ku_liao_cheng_item_cang_ku_name, parent));
            case TYPE_FOOTER:
                return new FooterViewHolder(inflate(R.layout.zhi_neng_cang_ku_liao_cheng_item_cang_ku_footer, parent));
            case TYPE_CANG_KU_PRODUCTS:
            default:
                return new ProductsViewHolder(inflate(R.layout.zhi_neng_cang_ku_liao_cheng_item_cang_ku_products, parent));
        }
    }

    @Override
    public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_CANG_KU_NAME:
                ((ShangJiaNameViewHolder) holder).bind(position);
                break;
            case TYPE_FOOTER:
                ((FooterViewHolder) holder).bind(position);
                break;
            case TYPE_CANG_KU_PRODUCTS:
            default:
                ((ProductsViewHolder) holder).bind(position);
                break;
        }
    }

    public void setDataItems(List<OrderListItem> orderListItems) {
        setItems(orderListItems);
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    public interface ChuKuInterface {
        void saoMiaoWuLiao(String out_storageList_id);

        void editSheZhiHeDuiXuHao(String out_storageList_id, String heDuiXuHao);

        void saoHuanChongWei(String out_storageList_id);

        void yunHui(String out_storageList_id, String mid);

        void wanCheng(String mid, String out_storageList_id, String taskId, String scanMid, String scanInstoreBufferId);

        void quXiaoNaHuo(String mid, String out_storageList_id, String taskId, String scanMid, String scanInstoreBufferId);

        void chuKu(String out_storagelist_batchid);

        void setScanMid(String scanMid);

        void setScanInstoreBufferId(String scanInstoreBufferId);

        void finishErrorTask(String out_storageList_id);

        void resetOutStorageListScanStatus(String out_storageList_id);
    }

    public static class OrderListItem extends ExpandableRecyclerAdapter.ListItem {
        public LiYiChuKuList mOutOfTheLibraryList;
        public LiYiChuKuList.OutListBean mOutListBean;
        public int mGroupPosition;
        public int mChildPosition;
        public String mOutStoragelistBatchid;

        public OrderListItem(LiYiChuKuList outOfTheLibraryList) {
            super(TYPE_CANG_KU_NAME);
            mOutOfTheLibraryList = outOfTheLibraryList;
        }

        public OrderListItem(int groupPosition, LiYiChuKuList.OutListBean outListBean, int childPosition, String outStoragelistBatchid) {
            super(TYPE_CANG_KU_PRODUCTS);
            mOutListBean = outListBean;
            mGroupPosition = groupPosition;
            mChildPosition = childPosition;
            mOutStoragelistBatchid = outStoragelistBatchid;
        }

        public OrderListItem(String first, String last, String footer, String type) {
            super(TYPE_FOOTER);
        }
    }

    public class ShangJiaNameViewHolder extends ExpandableRecyclerAdapter.ViewHolder {

        private TextView tv_name;
        private Button tv_zhuang_tai;
        private CheckBox cb_cang_ku_name;

        public ShangJiaNameViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            cb_cang_ku_name = view.findViewById(R.id.cb_cang_ku_name);
            tv_zhuang_tai = view.findViewById(R.id.tv_zhuang_tai);
        }

        public void bind(int position) {
            LiYiChuKuList outOfTheLibraryList = visibleItems.get(position).mOutOfTheLibraryList;
            tv_name.setText(outOfTheLibraryList.getOut_storagelist_batchid());

            final LiYiChuKuList group = (LiYiChuKuList) getGroup(mGroupsPosition.get(position));
            cb_cang_ku_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    group.setChoosed(((CheckBox) v).isChecked());
                    checkInterface.checkGroup(mGroupsPosition.get(position), ((CheckBox) v).isChecked());// 暴露组选接口
                }
            });

//            cb_cang_ku_name.setChecked(group.isChoosed());
            tv_zhuang_tai.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mChuKuInterface.chuKu(outOfTheLibraryList.getOut_storagelist_batchid());
                    tv_zhuang_tai.setClickable(false);
                    tv_zhuang_tai.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                }
            });

            if (outOfTheLibraryList.getSendOutOrder().equals("1")) {
                tv_zhuang_tai.setVisibility(View.VISIBLE);
                tv_zhuang_tai.setClickable(false);
                tv_zhuang_tai.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            }

        }
    }

    public class ProductsViewHolder extends ExpandableRecyclerAdapter.ViewHolder {

        private TextView mTvName;
        private TextView tv_num;
        private TextView tv_sao_mao;
        private EditText tv_shu_ru_wu_liao;
        private Button tv_sao_yun_hui;
        private Button tv_sao_wu_liao;
        private Button tv_sao_huan_chong_wei;
        private Button tv_qu_xiao_na_huo;
        private Button tv_chong_zhi_sao_miao;
        private CheckBox cb_product_name;
        private TextView outBufferName;
        private TextView spname;

        public ProductsViewHolder(View view) {
            super(view);
            mTvName = view.findViewById(R.id.tv_name);
            tv_num = view.findViewById(R.id.tv_num);
            outBufferName=view.findViewById(R.id.outBufferName);
            spname=view.findViewById(R.id.spname);
            cb_product_name = view.findViewById(R.id.cb_product_name);
            tv_shu_ru_wu_liao = view.findViewById(R.id.tv_shu_ru_wu_liao);
            tv_sao_mao = view.findViewById(R.id.tv_sao_mao);
            tv_sao_yun_hui = view.findViewById(R.id.tv_sao_yun_hui);
            tv_sao_wu_liao = view.findViewById(R.id.tv_san_wu_liao);
            tv_sao_huan_chong_wei = view.findViewById(R.id.tv_san_huan_chong_wei);
            tv_qu_xiao_na_huo = view.findViewById(R.id.tv_qu_xiao_na_huo);
            tv_chong_zhi_sao_miao = view.findViewById(R.id.tv_chong_zhi_sao_miao);
        }

        @SuppressLint("LongLogTag")
        public void bind(int position) {
            LiYiChuKuList.OutListBean outListBean = visibleItems.get(position).mOutListBean;
            mTvName.setText(outListBean.getMaterial());
            tv_num.setText(outListBean.getTaskStatus());
//            outBufferName.setText(outListBean.getOutBufferName());
//            Log.e("11111111111111111-------OutBufferName------",outListBean.getOutBufferName());
//            spname.setText(outListBean.getSpname());
//            Log.e("11111111111111111-----Spname--------",outListBean.getSpname());
            int groupPosition = visibleItems.get(position).mGroupPosition;
            int childPosition = visibleItems.get(position).mChildPosition;
            String outStoragelistBatchid = visibleItems.get(position).mOutStoragelistBatchid;

            final LiYiChuKuList.OutListBean goodsInfo2 = (LiYiChuKuList.OutListBean) getChild(groupPosition, childPosition);

            //设置已扫描的mid
            mChuKuInterface.setScanMid(outListBean.getMid());
            //设置已扫描的缓冲位id
            mChuKuInterface.setScanInstoreBufferId(outListBean.getScanbufferId());


            tv_shu_ru_wu_liao.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    String etNum = tv_shu_ru_wu_liao.getText().toString();
                    if (!TextUtils.isEmpty(etNum)) {
                        tv_sao_wu_liao.setClickable(false);
                        tv_sao_wu_liao.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));


                        //如果在300毫秒之内又输入了，就会再次返回，然后就再次remove掉了上次
                        //如果300毫秒内不输入了，就不会执行remove了，就直接跳转了
                        //原理：每次都执行跳转流程，但是有个条件，下一次不会返回的情况下，下次一旦返回就把上次的跳转操作取消了
                        if (runnable != null) {
                            handler.removeCallbacks(runnable);
                            Log.v("tag", "---" + s.toString());
                        }
                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                Log.v("tag", "跳转======" + s.toString());
                                mChuKuInterface.editSheZhiHeDuiXuHao(outListBean.getOut_storageList_id(), etNum);
                            }
                        };
                        Log.v("tag", "(((((" + s.toString());
                        handler.postDelayed(runnable, 3000);


                    } else {

                        if (outListBean.getScanBuffer().equals("1")) {
                            tv_sao_huan_chong_wei.setVisibility(View.VISIBLE);
                            tv_sao_huan_chong_wei.setClickable(false);
                            tv_sao_huan_chong_wei.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        } else if (outListBean.getScanBuffer().equals("0")) {
                            tv_sao_huan_chong_wei.setClickable(true);
                            tv_sao_huan_chong_wei.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                        }
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });


//            cb_product_name.setChecked(goodsInfo2.isChoosed());

//            if (goodsInfo2.getIsbeginscan().equals("1")) {
//                tv_sao_mao.setText("已扫描");
//            } else {
//                tv_sao_mao.setText("未扫描");
//            }

            cb_product_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    goodsInfo2.setChoosed(((CheckBox) v).isChecked());
//                    cb_product_name.setChecked(((CheckBox) v).isChecked());
//                    checkInterface.checkChild(groupPosition, position, ((CheckBox) v).isChecked());// 暴露子选接口

                }
            });


            //task_status when null then '未进行'
            //when 0 then '已计划'
            //when 1 then '进行中'
            //when 2 then '进行中'
            //when 3 then '进行中'
            //when 4 then '已入位'
            //when 6 then '待运回'
            if (outListBean.getTaskStatus().equals("待运回")) {
                tv_sao_yun_hui.setText("运回");
                tv_sao_wu_liao.setVisibility(View.GONE);
                tv_sao_huan_chong_wei.setVisibility(View.GONE);
                tv_sao_yun_hui.setVisibility(View.VISIBLE);
                tv_sao_yun_hui.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_sao_yun_hui.setClickable(false);
                        tv_sao_yun_hui.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mChuKuInterface.yunHui(outListBean.getOut_storageList_id(), outListBean.getMid());

                    }
                });


                if (outListBean.getBackOrder().equals("1")) {
                    tv_sao_yun_hui.setVisibility(View.VISIBLE);
                    tv_sao_yun_hui.setClickable(false);
                    tv_sao_yun_hui.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                }


            } else if (outListBean.getTaskStatus().equals("已入位")) {
                tv_sao_wu_liao.setVisibility(View.VISIBLE);
                tv_sao_huan_chong_wei.setVisibility(View.VISIBLE);
                tv_sao_yun_hui.setVisibility(View.VISIBLE);
                tv_chong_zhi_sao_miao.setVisibility(View.VISIBLE);
                tv_shu_ru_wu_liao.setVisibility(View.VISIBLE);
                tv_sao_yun_hui.setText("完成");
                tv_sao_yun_hui.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mChuKuInterface.wanCheng(outListBean.getScanMid(), outListBean.getOut_storageList_id(), outListBean.getTaskID(), outListBean.getMid(), outListBean.getScanbufferId());
                    }
                });
                //取消拿货
                tv_qu_xiao_na_huo.setVisibility(View.VISIBLE);
                tv_qu_xiao_na_huo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_qu_xiao_na_huo.setClickable(false);
                        tv_qu_xiao_na_huo.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mChuKuInterface.quXiaoNaHuo(outListBean.getScanMid(), outListBean.getOut_storageList_id(), outListBean.getTaskID(), outListBean.getMid(), outListBean.getScanbufferId());
                    }
                });
                if (outListBean.getBackOrder().equals("1")) {
                    tv_qu_xiao_na_huo.setVisibility(View.VISIBLE);
                    tv_qu_xiao_na_huo.setClickable(false);
                    tv_qu_xiao_na_huo.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                }
                //重置扫描状态
                tv_chong_zhi_sao_miao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mChuKuInterface.resetOutStorageListScanStatus(outListBean.getOut_storageList_id());
                    }
                });
//                if (outListBean.getOutOrder().equals("1")) {
//                    tv_sao_yun_hui.setVisibility(View.VISIBLE);
//                    tv_sao_yun_hui.setClickable(false);
//                    tv_sao_yun_hui.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
//                }
            } else if (outListBean.getTaskStatus().equals("未知") || outListBean.getTaskStatus().equals("未进行")) {

                if (outListBean.getTaskStatusLiKu().equals("任务型错误") || outListBean.getTaskStatusLiKu().equals("故障型错误")) {
//                    tv_num.setText(outListBean.getTaskStatusLiKu());
//                    tv_num.setText("likuErrorMsg");
                    tv_num.setText(outListBean.getTaskStatusLiKu());
                    tv_sao_yun_hui.setVisibility(View.VISIBLE);
                    tv_sao_yun_hui.setText("手动处理完毕");
                    tv_sao_yun_hui.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mChuKuInterface.finishErrorTask(outListBean.getOut_storageList_id());
                        }
                    });
                } else {
                    tv_num.setText(outListBean.getTaskStatusLiKu());
                }
            }

            if (outListBean.getTaskStatus().equals("已入位")) {
                if (outListBean.getScanBuffer().equals("1")) {
                    tv_sao_huan_chong_wei.setVisibility(View.VISIBLE);
                    tv_sao_huan_chong_wei.setClickable(false);
                    tv_sao_huan_chong_wei.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                } else if (outListBean.getScanBuffer().equals("0")) {
                    tv_sao_huan_chong_wei.setClickable(true);
                    tv_sao_huan_chong_wei.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                }

                if (outListBean.getScanMaterial().equals("1")) {
                    tv_sao_wu_liao.setVisibility(View.VISIBLE);
                    tv_sao_wu_liao.setClickable(false);
                    tv_sao_wu_liao.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                } else if (outListBean.getScanMaterial().equals("0")) {
                    tv_sao_wu_liao.setClickable(true);
                    tv_sao_wu_liao.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                }
            }

            tv_sao_wu_liao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    mChuKuInterface.saoMiaoWuLiao(outStoragelistBatchid);
                    mChuKuInterface.saoMiaoWuLiao(outListBean.getOut_storageList_id());
                    tv_sao_wu_liao.setClickable(false);
                    tv_sao_wu_liao.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                }
            });


            tv_sao_huan_chong_wei.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    mChuKuInterface.saoHuanChongWei(outStoragelistBatchid);
                    mChuKuInterface.saoHuanChongWei(outListBean.getOut_storageList_id());
                    tv_sao_huan_chong_wei.setClickable(false);
                    tv_sao_huan_chong_wei.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                }
            });
        }
    }

    public class FooterViewHolder extends ExpandableRecyclerAdapter.ViewHolder {

        private Button btn_que_ding;

        public FooterViewHolder(View view) {
            super(view);
            btn_que_ding = view.findViewById(R.id.btn_que_ding);
        }

        public void bind(int position) {
            btn_que_ding.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mChuKuInterface.saoMiaoWuLiao("");
                }
            });
        }
    }
}
